<?php

class MateriaPrimaController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
     	 #$this->_helper->layout->disableLayout ();
		//$this->_helper->viewRenderer->setNoRender ();
		 echo "<span>MateriaPrimaController</span>";
		 echo $this->view->util()->registerScriptJSController($this->getRequest());
    }
			
	public function matprimaAction(){
		 echo $this->view->util()->registerScriptJSController($this->getRequest());
	}
	public function busquedajsonAction(){
		
		$this->_helper->getHelper ( 'ajaxContext' )->initContext ();
		
		//if ($this->getRequest ()->isXmlHttpRequest ()) {
			$this->_helper->viewRenderer->setNoRender ();
			$this->_helper->layout->disableLayout ();
			
			//$bq = $this->_request->getPost('term');
			$bq = strtolower($_GET["term"]);
			//if (!$bq) return;
			$cn = new Model_Data();
			$parameter[]=array("@pcod_matpri",'');
			$parameter[]=array("@pdesc_nom",$bq);
			$parameter[]=array("@pest_matpri",'1');
			$result_bus = $cn ->ejec_store_procedura_sql("Mmateriaprima_list", $parameter);
			
			$result = array();
			foreach ($result_bus as $value) {
				array_push($result, array("id"=>$value[0], "label"=>$value[1], "value" => strip_tags($value[1])));
				//array_push($result, array("id"=>$value[0], "label"=>$value[1], "value" => $value[1]));
				if (count($result) > 5)
					break;
			}
			echo json_encode($result);
			
		//}
		
	}
	

	
	public function busmatprimaAction(){
		$this->_helper->getHelper ( 'ajaxContext' )->initContext ();
		
		if ($this->getRequest ()->isXmlHttpRequest ()) {
			$this->_helper->viewRenderer->setNoRender ();
			$this->_helper->layout->disableLayout ();
			
			$upper = new Zend_Filter_StringToUpper();
			$upper->setEncoding('UTF-8');
			$tip = $this->_request->getPost('tip');
			$val = $upper->filter($this->_request->getPost('val'));
			$chk = $this->_request->getPost('chk');
			$cn = new Model_Data();
			$parameter[]=array("@pcod_matpri",($tip=='0' ? $val:''));
			$parameter[]=array("@pdesc_nom",($tip=='1' ? $val:''));
			$parameter[]=array("@pest_matpri",$chk);
			$result_bus = $cn ->ejec_store_procedura_sql("Mmateriaprima_list", $parameter);
	

			if($result_bus==null || count($result_bus)==0){
				echo "Sin Resultados";
			}else{
				$tb="";
				foreach ($result_bus as $value) {
					$tb .= '<tr id="'.$value[0].'">';
					$tb .='<td>'.$value[0].'</td>';
					$tb .='<td>'.$value[1].'</td>';
					$tb .='<td>'.$value[3].'</td>';
					$tb .='<td>'.$value[4].'</td>';
					$tb .= '</tr>';
				}
				//echo '<table border="1"><tr><td>';
				echo '<table id="tbl_result" class="ui-widget ui-widget-content tablesorter">'.
						'<thead>'.
							'<tr class="ui-widget-header">'.
								'<th width="100px">Cod.Mat Prima</th>'.
								'<th width="279px">Nombre</th>'.
								'<th width="120px">Unid.Medida</th>'.
								'<th width="120px">StockMin</th>'.
							'</tr>'.
						'</thead>'.
						'<tbody>'.
							$tb.
						'</tbody>'.
					'</table>';

				//echo "</td></tr><tr><td>";
				echo $this->view->TableSorter()->ScriptDivPage();
				//echo $this->view->util()->registerScriptJSController($this->getRequest());
				//echo "</td></tr></table>";
				//$(\"#tbl_result\").tablesorter({sortList:[[0,0]], widgets: ['zebra'],headers: { 5:{sorter: false}}}).tablesorterPager({container: $(\"#pager\")});
				echo "<script type=\"text/javascript\">
						$(function() {		
							$(\"#tbl_result\").tablesorter({sortList:[[0,0]]})
							$(\"#tbl_result\").tablesorterPager({
								container: $(\"#pager\"),
								afterPagingCallBack: function(){
									AbrirMenuMateriaPrima();
								}
								});
							
						});	
					</script>";
				
				
			}
			
		}
	}
	public function mantematprimaAction(){
		
     	echo $this->view->util()->registerScriptJSController($this->getRequest());
		$this->_helper->layout->disableLayout ();
		#$this->_helper->viewRenderer->setNoRender ();
		$type = $this->_request->getParam('type', '');
		$codMatePrima = $this->_request->getParam('codItem', '');
		$parameter[]=array("@pcod_matpri",$codMatePrima);
		$parameter[]=array("@pdesc_nom",'');
		$parameter[]=array("@pest_matpri",'');
		$cn = new Model_Data();
		$pintar = New Libreria_Pintar();
		$UMparameter[]=array("@pcod_present","");
		$UMparameter[]=array("@pdesc_nom",'');
		$UMparameter[]=array("@pest_present",'1');
		$UMresult_bus = $cn ->ejec_store_procedura_sql("MPresentacion_list", $UMparameter);
		for ($x=0;$x<count($UMresult_bus);$x++)
    		$cboUM[] = array($UMresult_bus[$x][0],$UMresult_bus[$x][1]);

		if ($type=='N'){
			$val[] = array('#cboid_unidmed',$pintar->ContenidoCombo($cboUM,'null'),'html');	
			$val[] = array('#cboid_prese',$pintar->ContenidoCombo($cboUM,'null'),'html');
			$val[] = array('#cboid_equiv',$pintar->ContenidoCombo($cboUM,'null'),'html');
			
		}elseif($type=='M' || $type=='E'){
			$result_bus = $cn ->ejec_store_procedura_sql("Mmateriaprima_list", $parameter);
			$val[] = array('#cboid_unidmed',$pintar->ContenidoCombo($cboUM,$result_bus[0][2]),'html');
			$val[] = array('#hdid_matprima',$result_bus[0][0],'val');
			$val[] = array('#txtds_nombre',$result_bus[0][1],'val');
			$val[] = array('#nu_stockmin',$result_bus[0][4],'val');
			
			$parameterdt[]=array("@pcod_matpri",$codMatePrima);
			$parameterdt[]=array("@pest_equiv",'1');
			$result_det = $cn ->ejec_store_procedura_sql("Mequivalencia_list", $parameterdt);
			$detallemp='';
			if (count($result_det)==0){
				$val[] = array('#cboid_prese',$pintar->ContenidoCombo($cboUM,'null'),'html');
				$val[] = array('#cboid_equiv',$pintar->ContenidoCombo($cboUM,'null'),'html');
			}else{
			for ($i=0;$i<count($result_det);$i++){
				$detallemp .=
				'<tr >'.
					'<td><select name="det['.$i.'][cboid_prese]" id="cboid_prese"  class="ui-widget-content ui-corner-all">'.$pintar->ContenidoCombo($cboUM,$result_det[$i][3]).'</select></td>'.
					'<td><select name="det['.$i.'][cboid_equiv]" id="cboid_equiv"  class="ui-widget-content ui-corner-all">'.$pintar->ContenidoCombo($cboUM,$result_det[$i][5]).'</select></td>'.
					'<td align="right">'.
					'<input  name="det['.$i.'][cant]" type="text" class="num"  style="width: 70px" value="'.$result_det[$i][7].'">'.
					'</td>'.
					'<td style="display:none" ><input  name="det['.$i.'][tblcodigo]" id="tblcodigo" type="text" value="'.$result_det[$i][0].'"></td>'.
				'</tr>';
			}
				$val[] = array('#tbl_detalleMP tbody',$detallemp,'html');
			}
		}/*elseif($type=='E'){
			$result_bus = $cn ->ejec_store_procedura_sql("Mmateriaprima_list", $parameter);
			$val[] = array('#cboid_unidmed',$pintar->ContenidoCombo($cboUM,$result_bus[0][2]),'html');	
			$val[] = array('#hdid_matprima',$result_bus[0][0],'val');
			$val[] = array('#txtds_nombre',$result_bus[0][1],'val');
			$val[] = array('#nu_stockmin',$result_bus[0][4],'val');
		}*/
		
					
		
		$pintar->PintarValor($val);
		echo '<script type="text/javascript">'.
						'$("#tbl_detalleMP").grider({'.
						'countRow : true,'.
						'countRowAdd : true'.
						"});</script>";
	}

	public function executemanteAction(){
		$this->_helper->getHelper ( 'ajaxContext' )->initContext ();
		
		if ($this->getRequest ()->isXmlHttpRequest ()) {
			$this->_helper->viewRenderer->setNoRender ();
			$this->_helper->layout->disableLayout ();
			
			$upper = new Zend_Filter_StringToUpper();
			$upper->setEncoding('UTF-8');
			$tip = $this->_request->getPost('type');
			$id_matpri = $upper->filter($this->_request->getPost('id_matpri'));
			$ds_nombre = $upper->filter($this->_request->getPost('ds_nombre'));
			$id_unidmed = $upper->filter($this->_request->getPost('id_unidmed'));
			$nu_stock = $upper->filter($this->_request->getPost('nu_stock'));
			$st_estado = $upper->filter($this->_request->getPost('st_stado'));
			$arrtable = $this->_request->getPost('det');
			//$arrtable = json_decode($arrtable);
			//print_r($arrtable);
			
			//echo "<br>";
			$cn = new Model_Data();
			
			if($tip=='M' ||$tip=='N' ){
				$parameter[]=array("@ptype",$tip);
				$parameter[]=array("@pcod_matpri",$id_matpri);
				$parameter[]=array("@pdesc_nom",$ds_nombre);
				$parameter[]=array("@pcod_unidmed",$id_unidmed);
				$parameter[]=array("@pctd_stockmin",$nu_stock);
				$parameter[]=array("@pest_matpri",$st_estado);
				$parameter[]=array("@puser_reg",$this->view->util()->getUser());
				$parameter[]=array("@phost_reg",$this->view->util()->getHost());
				$xml='';
				$i=1;
				foreach ($arrtable as $value) {
					$xml.='';
					$xml .='<r ';
					$xml .='codigo="'.$value["tblcodigo"].'" ';
					$xml .='correla="'.($i).'" ';
					$xml .='cod_matpri="'.$id_matpri.'" ';
					$xml .='cod_presentorig="'.$value["cboid_prese"].'" ';
					$xml .='cod_presentequiv="'.$value["cboid_equiv"].'" ';
					$xml .='ctd_equiv="'.$value["cant"].'" ';
					$xml .='>';
					$xml .='</r>';
					$i++;
				}
				
				$xml="<d>".$xml."</d>";
				//echo "<textarea>".$xml."</textarea>";
				$parameter[]=array("@pdetalle",$xml);
				$result = $cn ->ejec_store_procedura_sql("Mmateriaprima_InsertUpdate", $parameter);
				 	
			}elseif($tip=='D'){
	
				$parameter[]=array("@pcod_matpri",$id_matpri);
				$parameter[]=array("@puser_reg",$this->view->util()->getUser());
				$parameter[]=array("@phost_reg",$this->view->util()->getHost());
				$result = $cn ->ejec_store_procedura_sql("Mmateriaprima_Delete", $parameter);
			}
			
			if($result[0][0]=='1') echo "Transaccion Exitosa";
			if($result[0][0]=='2') 
			 echo  $this->view->util()->getTextError($result[0]);
		}
		
	}
}